home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Commodore Free 10
/
Commodore_Free_Issue_10_2007_Commodore_Computer_Club.d64
/
t.slang tut2
< prev
Wrap
Text File
|
2023-02-26
|
11KB
|
407 lines
u SLANG TUTORIAL PART 2
Tutorial lesson #9:
What we are going to use is a simple
source code file. Try typing in the
following code, and compile and run
it:
byte b for b=1:10 println "b=" b next
print "press any key..." waitchar
done
put 'putcore.e.s'
The two critical differences here are
1) we are now using print instead of
sprint, and 2) the "put" command at
the end of the file.
For now, instead of going into detail
on PUT, let's just say that you need
that line at the DendD of your code
-- after the done statement -- if you
want to use the full print command.
Notice that we have put both a string
and a number on the print line,
similar to BASIC. You can also
separate these with commas, if you
like:
print "b=",b
It just depends on which way you
think is clearer. You can also print
to any part of the screen:
print(b,20) "this is row "b
Tutorial lesson #10:
Replace the println statment with the
above "this is row" statement, and see
what happens.
As you can see, print is much more
powerful and flexible than sprint, but
the downside is that you need to PUT
the core library in there, which
increases compile time and makes the
program much larger. Experience will
help you figure out when you want to
use one or the other.
The core library Above we used the
core library, using the line put
"putcore.e.s" at the end of the
program (whether you use ' or " quotes
doesn't matter, incidentally). This is
a very important library and is needed
for more than just print.
Tutorial lesson #11:
Multiplication and division of
bytes/ints requires this library, and
you will get an error if you don't
include the core library. (Print will
also generate an error.) Try the
following program:
int b b=10 b=b*2 done
Now compile it, and you will get an
error at the multiplication. Then add
the line
put 'putcore.e.s'
to the end of the program, compile...
and it will work. And since you're
including the core library anyways,
you can go ahead and print out b if
you want to.
Why use a library? If you know BASIC,
you know that a command like "print"
actually calls a routine in the BASIC
ROMs to do its thing. The BASIC ROMs
are, for the most part, one big
library, but they are in ROM instead
of in a disk file.
Sometimes machine language programmers
will call these BASIC routines instead
of writing their own. Similarly, in a
compiled language like Slang,
sometimes it makes sense to call a
common routine to perform some task. A
library is nothing more than a
collection of useful routines.
Arrays and Strings We're just about
done here. I'm not going to cover
DallD of the available commands; the
goal here is to get across the major
commands, and then youcan browse the
slangref.txt document to check out
other commands. So I'll just touch on
the remaining topics at this point.
Arrays in Slang work much the same as
arrays in BASIC. You declare them very
similarly to other variables:
int b ;regular variable int c(20)
;array
This is similar to using the DIM
statement, if that helps. There's just
one thing to remember: array indices
start at 0. In the above declaration,
you can address c(0), c(1), c(2), ...
c(19), like
c(19) = 1000
but
c(20) = 1000
will be incorrect (and may cause a
crash). There are 20 elements total: 0
through 19. If it's too confusing,
then one thing you can do is to add
one extra element to any array --
like, use "int c(21)" -- and not worry
about it.
Strings also are really similar to
BASIC, but you're going to have to get
one thing straight in your head:
strings are really just bytes. There
is no "string" type, like
byte/int/float; strings are just
bytes.
You already know this, from BASIC:
d$="a" ;treating letter "a" as a
string d$=chr$(65) ;treating "a" as
the number 65
The letter "a" is really just a number
-- 65 in this case. This works the
same way in Slang:
ubyte d d = "a" ;a "string" d = 65 ;a
number
That's fine for just one character,
but that's not a string. In Slang, a
string is just a byte array:
ubyte d(20)
d$ = "hello" ;treat as a string d(0)
= "y" ;change string to "yello" d(0)
= 67 ;change string to "cello"
In this example, d is a byte array.
You can treat it as a string, or as a
list of numbers. As in BASIC, the "$"
tells Slang to treat the variable as a
string -- I won't go into the details
about this, but I think the meaning is
pretty clear from the above example.
Tutorial lesson #12
Here's a simple program using strings
to try out:
ubyte d(20),i d$="hello" println
"d$=" d$ d(0) = 67 println "d$=" d$
for i=0:5
println d(i) endfor waitchar done
Go ahead and play around with it --
try adding 1 to each element of the
string, etc. One important thing to
note: when you run the program, you'll
notice that the last number printed in
the for-loop is a 00. With strings,
the very last element will be zero --
this is what tells Slang where the end
of the string is. If you overwrite
this ending zero, you'll generally get
a whole bunch of garbage. So.. .
Tutorial lesson #13
Change the line
d(0) = 67
to
d(5) = 67
and see what happens when the string
is printed. This overwrites the ending
zero byte, and should in general print
out a bunch of garbage. Sometimes, if
you're lucky, there will be another 00
somewhere in the array!
If-elseif-endif The if-then
structure:
if a=10 do something elseif a=11 do
something else endif
It's similar to the BASIC command,
except that you have an elseif or
endif command to mark the block of
code to be executed, just like while
and repeat.
Subroutines In BASIC, you are familiar
with GOSUB -- it calls a subroutine,
and then RETURN returns back to the
place of the subroutine call. Slang
subroutines are more sophisticated,
but the idea is the same and overall
they are pretty similar.
A subroutine is like a mini-program:
you can define variables, have a bunch
of statements, etc. The neat thing is
that these variables and statements
are DlocalD to the subroutine -- they
"belong" to the subroutine, and do not
interact with other subroutines. This
allows you to organize your programs
efficiently.
Just like a variable, you have to
define a subroutine. And you need to
end the routine using "endsub" .
With a subroutine, you can pass
DparametersD to the routine. For
example, the graphics library has a
routine called GrPlot, to plot a
point. And it requires two parameters:
the x- and y-position of the point to
be plotted:
GrPlot(x1,y1)
Subroutines can also pass parameters
DbackD to the calling routine. Once
you "get" the idea of parameter
passing, and of local variables, the
rest is a cinch.
Tutorial lesson #14:
Type in and run the following program,
which demonstrates the idea of local
variables within a subroutine:
byte b b=1 TestRoutine() ;Call the
subroutine println "but in the main
code, b is still "b waitchar done
sub TestRoutine() ;Define the
subroutine
byte b ;create the DlocalD variable b
b=10
println "in the subroutine, b="b
endsub ;end subroutine
put "putcore.e.s"
You can see what I mean about a
subroutine being a mini-program.
Within the subroutine you declare
variables, have statements, and end it
with an endsub. You don't really worry
about what other routines or the main
program does; the subroutine doesn't
"see" them.
So, a few observations: the "sub"
keyword defines a subroutine. We
DcallD the subroutine by simply typing
the subroutine name in the main
program. Once the routine finishes,
the calling program continues
executing where it left off.
The subroutine declares its own
variable b. Even though it has the
same name as the variable in the main
program, this is a DlocalD variable;
it "belongs" to the subroutine, not to
the main program.
Tutorial lesson #15
In this lesson, we'll try parameter
passing.
int x1,y1 x1=12 y1=20 AddEm(x1,y1)
println "the result was "
AddEm<-result waitchar done
sub AddEm(int a, int b)<-int result
result = a+b endsub
put "putcore.e.s"
(As before, <- is the backarrow key)
In this example, the subroutine AddEm
takes two DinputD parameters, a and b,
and has one DoutputD parameter,
result. As before, all three of
theseparameters are local to the
subroutine, so they could have the
same name as variables in the main
program.
With the return parameter, what we are
really doing is making a subroutine
variable DvisibleD to outside
routines. Normally all variables and
such inside a subroutine belong to the
subroutine, and are not available
outside of the routine; this is how to
make specific ones available outside
of the subroutine. Just as with input
variables, there can be a whole list
of output variables.
Back in the main program, this is just
another variable called AddEm<-result.
You can use it in expressions, etc.
just like any other variable:
x1 = x1 + 2*AddEm<-result
So in summary: a subroutine is like a
mini-program, with its own variables
and statements. But, you can pass
parameters into the subroutine, and
youcan retrieve variables out of the
subroutine, as needed. Subroutines are
really helpful in organizing and
simplifying programs, so it's well
worth taking the time to understand
them if you don't already!
Saving object code Once you've written
a program, what do you do with it?
What we're going to do here is save
the DobjectD code, and then load and
run it.
Tutorial lesson #16
(always need some multiple of 16,
right? )
Compile one of the example programs
above (one that works!). Once it
compiles successfully, press F7 to
enter the disk menu. First, save the
source code by pressing "s". Go ahead
and enter some filename; the program
will automatically append a ".s" to
the filename.
Second, save the object code by
pressing "o". You'll see the same
filename as default, so go ahead and
press return. If you now list the
directory, you should see two new
files: one with a .s, the other with a
.o. You can now load and run the
object code totally indepently from
Slang.
Exit back to the editor. We are now
going to exit to BASIC: press
shift-ctrl- < (backarrow, to the left
of the '1' key), and you should be
back at the BASIC prompt. If you'd
like to try your program from here,
you can type "sys 4096" .
Now reset the machine. Go to the disk
directory, and load your object file
,8,1.
Your object code is a machine language
file, just like other programs you may
own or download. Once it's loaded,
type "sys 4096" to run it. (By
default, Slang programs are located at
4096, but it is easy to loca
G